package cn.wolfcode.crm.service.impl;

import cn.wolfcode.crm.domain.SystemDictionaryItem;
import cn.wolfcode.crm.mapper.SystemDictionaryItemMapper;
import cn.wolfcode.crm.qo.QueryObject;
import cn.wolfcode.crm.qo.SystemDictionaryItemQueryObject;
import cn.wolfcode.crm.service.ISystemDictionaryItemService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class SystemDictionaryItemService implements ISystemDictionaryItemService {
    @Autowired
    private SystemDictionaryItemMapper systemDictionaryItemMapper;
    @Override
    public void saveOrUpdate(SystemDictionaryItem systemDictionaryItem) {
        if(systemDictionaryItem.getId() != null){
            systemDictionaryItemMapper.updateByPrimaryKey(systemDictionaryItem);
        } else {
            systemDictionaryItemMapper.insert(systemDictionaryItem);
        }
    }

    @Override
    public void delete(Long id) {
        if(id != null){
            systemDictionaryItemMapper.deleteByPrimaryKey(id);
        }
    }

    @Override
    public SystemDictionaryItem getSystemDictionaryItemById(Long id) {
        return systemDictionaryItemMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<SystemDictionaryItem> getAll() {
        return systemDictionaryItemMapper.selectAll();
    }

    @Override
    public PageInfo<SystemDictionaryItem> selectByQuery(SystemDictionaryItemQueryObject queryObject) {
//        int totalCount = systemDictionaryItemMapper.countByQuery(queryObject);
//        if(totalCount == 0){
//            return new PageResult(1,queryObject.getPageSize(),0, Collections.emptyList());
//        }
        //用pageInfo   调用方法
        if(queryObject.getParentId() == null){
            return null;
        }else {
            PageHelper.startPage(queryObject.getCurrentPage(),queryObject.getPageSize());
            List<SystemDictionaryItem> systemDictionaryItems = systemDictionaryItemMapper.selectByQuery(queryObject);
            return new PageInfo<>(systemDictionaryItems);
        }

    }

    @Override
    public List<SystemDictionaryItem> selectBySystemDictionarySn(String sn) {
        return systemDictionaryItemMapper.selectBySystemDictionarySn(sn);
    }
}
